<template>
    <h1>借款信息修改页面</h1>

    <table>
        <tr>
            <td>借款名称</td>
            <td>
                <input type="text" v-model="Borrow.BM_Name" >
            </td>
        </tr>

        <tr>
            <td>借款最高年限</td>
            <td>
                <input type="text" v-model="Borrow.BM_Year" >
            </td>
        </tr>

        <tr>
            <td>借款最高额度</td>
            <td>
                <input type="text" v-model="Borrow.BM_Money" >
            </td>
        </tr>

        <tr>
            <td>借款介绍</td>
            <td>
                <textarea v-model="Borrow.BM_Info" >

                </textarea>
            </td>
        </tr>

        <tr>
            <td>借款利息</td>
            <td>
                <select v-model="Borrow.BI_Id" >
                    <option value="0">--请选择--</option>
                    <option v-for="item in Interest" :key="item.BI_Id" :value="item.BI_Id">
                        {{ item.BI_Year }}年{{ item.BI_Interest }}%
                    </option>
                </select>
            </td>
        </tr>

        <tr>
            <td colspan="2">
                <input type="button" value="修改" @click="Update" >
            </td>
        </tr>


    </table>

</template>

<script setup lang="ts">
import { ref,onMounted } from 'vue'
import axios from 'axios'
import { useRouter,useRoute } from 'vue-router';

const router = useRouter();
const route = useRoute();

console.log(route.params.Id);


const Borrow = ref({
    BM_Name:'',
    BM_Year:'',
    BM_Money:'',
    BM_Info:'',
    BI_Id:''
})

const Interest = ref([{
    BI_Id:1,
    BI_Year:'',
    BI_Interest:''
}])

onMounted(()=>{
    SaveInvest();
    SearData();
})

const SaveInvest = ()=>{
    axios({
        method:'get',
        url:'http://localhost:63517/api/Borrow/SaveInvest',
    })
    .then(function(res){
        console.log(res.data);
        Interest.value = res.data;
    })
    .catch(function(err){
        console.log(err);
    })
}

const SearData = ()=>{
    axios({
        method:'get',
        url:'http://localhost:63517/api/Borrow/SearData',
        params:{
            Id:route.params.Id
        }
    })
    .then(function(res){
        console.log(res.data);
        Borrow.value = res.data;
    })
    .catch(function(err){
        console.log(err);
    })
}

const Update = ()=>{
    axios({
        method:'post',
        url:'http://localhost:63517/api/Borrow/Update',
        data:Borrow.value
    })
    .then(function(res){
        console.log(res.data);
        if(res.data == 1){
            alert('修改成功');
            router.push('/ShowBorrow');
        }
        else{
            alert('修改失败');
        }
    })
    .catch(function(err){
        console.log(err);
    })
}


</script>
